<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-07-11
  Time: 11:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品评论</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <style>
        .onClick{
            color: white;
        }
        .layui-btn-group .layui-btn-primary:hover {
            border-color: #d2d2d2;
            color: rgb(30, 159, 255);
        }
        .layui-btn-group .layui-btn-normal:hover {
            border-color: #d2d2d2;
            color: white;
        }
        .layui-table-cell {
            text-align: center;
            white-space: normal;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-row" style="margin-top: 10px;margin-bottom: 10px">
            <div class="layui-inline">
                <div class="layui-input-block" style="margin-left: -9px;">
                    <label class="layui-form-label">时间选择</label>
                    <div class="layui-btn-group">
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnAll">全部</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnToday">今天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnYesterday">昨天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnLast7Days">最近7天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnLast30Days">最近30天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnThisMonth">本月</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnThisYear">本年</button>
                    </div>
                    <div class="layui-input-inline" style="width: 230px;">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                            <input type="text" class="layui-input" id="ID-laydate-shortcut-range-date" placeholder="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">评价星级</label>
                <div id="ID-rate-demo-text"></div>
            </div><br>
            <div style="margin-top: 15px;margin-left: -9px;">
                <div class="layui-inline" >
                    <label class="layui-form-label">用户昵称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="请输入用户昵称" class="layui-input" id = "userName">
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: 50px">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="goodsName" placeholder="请输入商品名称" class="layui-input" id = "goodsName">
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: 50px">
                    <button id="search" class="layui-btn layui-btn-normal layui-row-xs" lay-submit lay-filter="search">查找
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-row-xs">清空</button>
                </div>
            </div>
        </form>

        <table class="layui-hide" id="commentTableId" lay-filter="commentTableFilter"></table>

        <%-- 行内操作工具条--%>
        <script type="text/html" id="commentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="view">查看</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script>
    //查看商品详情
    function goodsDetails(goodsId) {
        var index = layer.open({
            title: '商品详情',
            type: 2,
            shade: 0.2,
            offset: 'lt',
            maxmin: true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: '${pageContext.request.contextPath}/manager/goods/details?id=' + goodsId,
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
    }
</script>
<script>
    layui.use(['form', 'table', 'element','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;
        var rate = layui.rate;
        var laydate = layui.laydate;

        // 获取所有按钮元素
        var btnAll = document.getElementById("btnAll");
        var btnToday = document.getElementById("btnToday");
        var btnYesterday = document.getElementById("btnYesterday");
        var btnLast7Days = document.getElementById("btnLast7Days");
        var btnLast30Days = document.getElementById("btnLast30Days");
        var btnThisMonth = document.getElementById("btnThisMonth");
        var btnThisYear = document.getElementById("btnThisYear");

        // 绑定点击事件处理函数
        btnAll.addEventListener("click", function() {
            var dateRange = "";
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(false,null);
        });

        btnToday.addEventListener("click", function() {
            var dateRange = getCurrentDate() + " - " + getCurrentDate();
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnYesterday.addEventListener("click", function() {
            var today = new Date();
            var yesterday = new Date(today);
            yesterday.setDate(yesterday.getDate() - 1);

            var dateRange = formatDate(yesterday) + " - " + formatDate(yesterday);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnLast7Days.addEventListener("click", function() {
            var endDate = new Date();
            var startDate = new Date(endDate);
            startDate.setDate(startDate.getDate() - 6);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnLast30Days.addEventListener("click", function() {
            var endDate = new Date();
            var startDate = new Date(endDate);
            startDate.setDate(startDate.getDate() - 29);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnThisMonth.addEventListener("click", function() {
            var today = new Date();
            var startDate = new Date(today.getFullYear(), today.getMonth(), 1);
            var endDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnThisYear.addEventListener("click", function() {
            var today = new Date();
            var startDate = new Date(today.getFullYear(), 0, 1);
            var endDate = new Date(today.getFullYear(), 11, 31);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        // 日期格式化函数
        function formatDate(date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();

            return year + "-" + formatTwoDigits(month) + "-" + formatTwoDigits(day);
        }

        // 格式化为两位数字
        function formatTwoDigits(number) {
            return ("0" + number).slice(-2);
        }

        // 获取当前日期
        function getCurrentDate() {
            var today = new Date();
            return formatDate(today);
        }

        //搜索事件
        function search(quick,quickTime) {
            const inputTimeElement = document.getElementById('ID-laydate-shortcut-range-date');
            let timeValue = inputTimeElement.value;
            if(quick == true)
                timeValue = quickTime;
            let starsValue = $('#ID-rate-demo-text').text(); // 获取<div>元素的文本值
            starsValue = parseFloat(starsValue);
            let startTime = null;
            let endTime = null;
            if(timeValue != null && timeValue !== ''){
                const dateString = timeValue;
                const dateArray = dateString.split(" - ");
                startTime = dateArray[0];
                endTime = dateArray[1];
            }
            if(starsValue == 0)
                starsValue = null;
            var userName = document.getElementById('userName').value;
            var goodsName = document.getElementById('goodsName').value;
            table.reload('commentTableId', {
                page: {
                    curr: 1 //重新从第 2 页开始
                }
                , where: {
                    stars: starsValue,
                    startTime:startTime,
                    endTime:endTime,
                    userName:userName,
                    goodsName:goodsName
                }
            }, 'data');
        }

        // 监听按钮点击事件
        $('.btn-time').on('click', function () {
            var id = this.id; // 获取按钮的id

            // 移除所有按钮的选中状态
            $('.btn-time').removeClass('layui-btn-normal onClick');
            // 设置当前按钮为选中状态
            $(this).addClass('layui-btn-normal onClick');
        });

        //日期范围
        laydate.render({
            elem: "#ID-laydate-shortcut-range-date",
            range: true,
            done:function(value){
                $('.btn-time').removeClass('layui-btn-normal onClick');
                search(false,null);
            },
            shortcuts: [
                {
                    text: "上个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        date1.setMonth(date1.getMonth() - 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                },
                {
                    text: "这个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        // date1.setMonth(date1.getMonth() - 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setMonth(date2.getMonth() + 1);
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                },
                {
                    text: "下个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        date1.setMonth(date1.getMonth() + 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setMonth(date2.getMonth() + 2);
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                }
            ]
        });

        // 渲染星级选择器
        rate.render({
            elem: '#ID-rate-demo-text',
            value: 0, // 初始值
            text: true, // 开启文本
            choose: function(){
                search(false,null)
            }
        });

        /**
         * 表格渲染
         */
        table.render({
            elem: '#commentTableId',
            autoSort: true,
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize' // 每页数据条数的参数名，默认：limit
            },
            url: '${pageContext.request.contextPath}/manager/comment/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            lineStyle: 'height: 100px', // 定义表格的多行样式
            cols: [[
                {type: "checkbox", width: 100,fixed: 'left'},
                {title: '序号', type: "numbers", width: 80, align: "center"},
                {field: 'id', width: 105, title: '编号', sort: true,hide:true},
                {field: 'content', width: 350, title: '评论内容', sort: true},
                {field: 'stars', width: 110, title: '星级', sort: true},
                {field: 'userId', width: 110, title: '用户id', sort: true},
                {field: 'userName', width: 100, title: '用户名字'},
                {field: 'goodsId', width: 140, title: '商品编号', sort: true},
                {
                    field: 'goodsName', width: 120, title: '商品名字',templet: function (d) {
                        return '<a class = "buttton" href="javascript:void(0)"  style="color: #1aa094" onclick="goodsDetails(' + d.goodsId + ')">' + d.goodsName + '</a>';
                    }
                },
                {field: 'priority', width: 120, title: '优先级',sort: true},
                {field: 'createTime', width: 120, title: '创建时间'},
                {fixed: 'right', title:'操作', width: 150, toolbar: '#commentTableBar'}
            ]],
            limits: [8, 15, 20, 25, 50, 100],
            limit: 8,
            page: true,
            loading: true,
            skin: 'grid',
        });

        form.on('submit(search)', function (data) {
            search(false,null);
            return false;
        });
    });
</script>
</body>
</html>
